package collection.queue.block_queue;

import org.junit.Test;

import java.util.Objects;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/16 7:42 PM
 */
public class DelayQueueTest {

    /**
     * @see DelayQueue
     */
    @Test
    public void test1() {
        DelayQueue<Node> queue = new DelayQueue();
        queue.offer(new Node(100L));
        queue.offer(new Node(55L));
        queue.offer(new Node(666L));

        System.out.println(queue);
    }


    private class Node implements Delayed {

        private Long count;

        public Long getCount() {
            return count;
        }

        public Node() {
        }

        public Node(Long count) {
            this.count = count;
        }

        @Override
        public long getDelay(TimeUnit unit) {
            return Objects.isNull(this.count) ? 0 : this.count;
        }

        @Override
        public int compareTo(Delayed o) {
            return Long.compare(this.getDelay(TimeUnit.MICROSECONDS), o.getDelay(TimeUnit.MICROSECONDS));
        }
    }

}
